package org.wjw.auth.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wjw.auth.mapper.LoginMapper;
import org.wjw.auth.service.ILoginService;
import org.wjw.base.util.LoginMap;
import org.wjw.dto.LoginDTO;
import org.wjw.org.domain.Employee;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
public class LoginServiceImpl implements ILoginService {
    @Autowired
    LoginMapper loginMapper;

    @Override
    public Map<String, Object> login(LoginDTO loginDTO) {
        if(loginDTO.getPassword()==null||loginDTO.getUsername()==null){
            throw new RuntimeException("用户名或者密码为空");
        }
        Employee employee=loginMapper.selectByUsername(loginDTO.getUsername());
        System.out.println("loginDTO"+loginDTO);
        System.out.println(employee);
        if(employee==null){
            throw new RuntimeException("用户名或密码输入错误");
        }
        System.out.println(loginDTO.getPassword());
        System.out.println(employee.getPassword());
        if(!loginDTO.getPassword().equals(employee.getPassword())){
            throw new RuntimeException("用户名或密码输入错误");
        }
        String token = UUID.randomUUID().toString();
        LoginMap.loginMap.put(token,employee);
        Map<String,Object> logMap=new HashMap<>();
        logMap.put("token",token);
        logMap.put("loginUser",employee);
        return logMap;
    }
}
